package per.hx.bs.yw.serviceimpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.constant.DictTpye;
import per.hx.bs.entity.yw.*;
import per.hx.bs.yw.dao.*;

import java.util.List;
import java.util.Map;

/**
 * @author HuXing
 * @date 2018/4/14  11:13
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping(value = "yw-service/appraisalApply")
public class YwAppraisalApplyApiServiceImpl {

    private final
    YwAppraisalApplyDao applyDao;

    private final
    YwAppraisalDataDao dataDao;

    private final
    YwAppraisalItemDao itemDao;

    private final
    YwAppraisalRecordDao recordDao;

    @Autowired
    public YwAppraisalApplyApiServiceImpl(YwAppraisalApplyDao applyDao, YwAppraisalDataDao dataDao, YwAppraisalItemDao itemDao, YwAppraisalRecordDao recordDao) {
        this.applyDao = applyDao;
        this.dataDao = dataDao;
        this.itemDao = itemDao;
        this.recordDao = recordDao;
    }

    /**
     * 获取需要审核的考核信息
     *
     * @param params 条件
     * @return 审核信息
     */
    @PostMapping(value = "/list")
    public List<YwAppraisalApplyDO> list(@RequestBody Map<String, Object> params) {
        return applyDao.list(params);
    }

    /**
     * 获取需要审核的种条数
     *
     * @param params 条件
     * @return 数量
     */
    @PostMapping(value = "/count")
    public int count(@RequestBody Map<String, Object> params) {
        return applyDao.count(params);
    }

    /**
     * 通过id获取数据
     *
     * @param id id
     * @return 申请信息
     */
    @GetMapping(value = "/{id}")
    public YwAppraisalApplyDO selectOne(@PathVariable("id") Long id) {
        return applyDao.selectById(id);
    }

    /**
     * 插入申请
     *
     * @param applyDO 申请信息
     * @return 影响行数
     */
    @PostMapping(value = "/insert")
    public int insert(@RequestBody YwAppraisalApplyDO applyDO) {
        if (applyDO.getAmount() == null) {
            applyDO.setAmount(1);
        }
        applyDO.setState(DictTpye.APPRAISAL_APPLY_WAITE_STATE);
        applyDO.setStateName(DictTpye.APPRAISAL_APPLY_WAITE_STATE_NAME);
        return applyDao.insert(applyDO);
    }

    /**
     * 更新申请
     *
     * @param applyDO 申请信息
     * @return 影响行数
     */
    @PutMapping(value = "/update")
    public int update(@RequestBody YwAppraisalApplyDO applyDO) {
        YwAppraisalApplyDO applyDO1 = applyDao.selectById(applyDO.getId());
        YwAppraisalRecordDO recordDO = new YwAppraisalRecordDO();
        recordDO.setTypeName(applyDO1.getItemOneName());
        YwAppraisalItemDO itemDO;
        if (applyDO1.getItemThree() != null) {
            itemDO = itemDao.selectById(applyDO1.getItemThree());
            recordDO.setItemName(applyDO1.getItemTwoName() + ":" + itemDO.getAppraisalContent());
            applyDO.setCheckMoney(applyDO.getAmount() * itemDO.getMoney());
        } else {
            itemDO = itemDao.selectById(applyDO1.getItemTwo());
            recordDO.setItemName(itemDO.getAppraisalContent());
            applyDO.setCheckMoney(applyDO.getAmount() * itemDO.getMoney());

        }
        applyDao.updateById(applyDO);

        recordDO.setItemId(itemDO.getId());
        recordDO.setItemMoney(itemDO.getMoney());
        recordDO.setItemType(itemDO.getMoneyType());
        recordDO.setItemUnit(itemDO.getMoneyUnit());
        recordDO.setItemRemark(itemDO.getRemark());
        recordDO.setRecordAmount(applyDO.getAmount());
        recordDO.setRecordMoney(applyDO.getCheckMoney());
        recordDO.setCreateId(applyDO.getUpdateId());
        recordDO.setFileId(applyDO1.getDataId());
        recordDO.setUserId(applyDO1.getUserId());
        return recordDao.insert(recordDO);
    }


    /**
     * 通过id获取数据
     *
     * @param id id
     * @return 申请信息
     */
    @GetMapping(value = "/data")
    public YwAppraisalDataDO selectOneData(@RequestParam("id") Long id) {
        return dataDao.selectById(id);
    }

    /**
     * 插入申请数据
     *
     * @param dataDO 申请信息
     * @return 回调结果
     */
    @PostMapping(value = "/insertData")
    public Long insertData(@RequestBody YwAppraisalDataDO dataDO) {
        if (dataDao.insert(dataDO) > 0) {
            return dataDO.getId();
        }
        return 0L;

    }

    /**
     * 更新申请数据
     *
     * @param dataDO 申请信息数据
     * @return 或吊结果
     */
    @PutMapping(value = "/updateData")
    public int updateData(@RequestBody YwAppraisalDataDO dataDO) {
        return dataDao.updateById(dataDO);
    }

}
